package com.lanou.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientUtils {

    public static <T> T getObject(String url, Class<T> clazz) {
        String rawStr = getRaw(url);
        JSONObject jsonObject = JSONObject.parseObject(rawStr);
        if (jsonObject.containsKey("data")) {
            return jsonObject.getObject("data", clazz);
        } else {
            return JSONObject.parseObject(rawStr, clazz);
        }
    }

    public static <T> List<T> getList(String url, Class<T> clazz) {
        String rawStr = getRaw(url);

        JSONObject jsonObject = null;
        try {
            jsonObject = JSONObject.parseObject(rawStr);
            JSONArray data = jsonObject.getJSONArray("data");
            return data.toJavaList(clazz);
        } catch (Exception e) {
            return JSONArray.parseArray(rawStr, clazz);
        }
    }

    public static String getRaw(String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        String s = null;
        try {
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            s = EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return s;
    }

    public static String post(String url, Map<String, Object> params) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpResponse response = null;
        String s = null;
        try {
            //装填参数
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            if (params != null) {
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
                }
            }
            //设置参数到请求对象中
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            s = EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return s;
    }
}
